package com.huatai.auth.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.api.utils.NetUtils;
import com.huatai.common.api.R;
import com.huatai.common.util.Kv;
import com.huatai.common.util.StringUtil;

import com.wf.captcha.SpecCaptcha;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;

import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @program: ht-system
 * @description:
 * @author: songxiude
 * @create: 2023-04-27 10:33
 **/
@RestController
@Api(value = "统一认证鉴权", tags = "统一认证鉴权")
public class AuthController {
	@Autowired
	private StringRedisTemplate redisTemplate;

	@Autowired
	private RestTemplate restTemplate;

	@Value("${server.port}")
	private String port;

	@GetMapping("/oauth/captcha")
	@ApiOperation(value = "获取图形验证码", notes = "获取图形验证码")
	public Kv captcha() {
		SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 5);
		String verCode = specCaptcha.text().toLowerCase();
		String key = StringUtil.randomUUID();
		// 存入redis并设置过期时间为30分钟
		redisTemplate.opsForValue().set("ht:captcha:"+key,verCode,30, TimeUnit.SECONDS);
		// 将key和base64返回给前端
		return Kv.create().set("key", key).set("image", specCaptcha.toBase64());
	}

	@PostMapping("/oauth/login")
	@ApiOperation(value = "登录", notes = "登录")
	public R login(@RequestParam("username") String username,@RequestParam("password") String password,@RequestHeader HttpHeaders httpHeaders){
		String url= "http://"+NetUtils.localIP()+":"+port+"/oauth/token";
		// 构造 post的body内容（要post的内容，按需定义）
		MultiValueMap<String, String> paramsMap = new LinkedMultiValueMap<>();
		paramsMap.set("grant_type", "password");
		paramsMap.set("username", username);
		paramsMap.set("client_id", "huatai");
		paramsMap.set("client_secret", "waytale");
		paramsMap.set("auth_type", "password");
		paramsMap.set("password", password);
		// 使用客户端的请求头,发起请求
		httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
		// 强制移除 原来的请求头,防止token失效
		httpHeaders.remove(HttpHeaders.AUTHORIZATION);
		//构造请求实体和头
		HttpEntity<MultiValueMap<String, String>> request = new HttpEntity(paramsMap, httpHeaders);
		JSONObject authInfo = null;
		try {
			authInfo = restTemplate.postForObject(url, request, JSONObject.class);
		} catch (HttpClientErrorException e) {
			return R.fail("登录失败");
		}

		return R.data(authInfo);
	}

//	@GetMapping("/oauth/getSignCode")
//	@ApiOperation(value = "获取软件授权签名", notes = "获取软件授权签名")
//	public R getSignCode(){
//		String systemSign = LicenseManager.getSystemSign();
//		return R.data(systemSign);
//	}
}
